#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
public_file_bt=/hcdemo/shellinstall/dlurl-bt.sh
public_file_hc=/hcdemo/shellinstall/dlurl-hc.sh
if [ ! -f $public_file_bt ];then
    mkdir -p /hcdemo/shellinstall/
	wget -O $public_file_bt https://dl.winqi.cn/install/install/dlurl-bt.sh -T 20;
fi
if [ ! -f $public_file_hc ];then
	wget -O $public_file_hc https://dl.winqi.cn/install/install/dlurl-hc.sh -T 20;
fi
. $public_file_bt			 
. $public_file_hc
download_Url_BT=${NODE_URL_BT}
download_Url_HC=${NODE_URL_HC}/install
redis_version=5.0.7
runPath=/root
System_Lib(){
	if [ "${PM}" == "yum" ] || [ "${PM}" == "dnf" ] ; then
		Pack="sudo"
		${PM} install ${Pack} -y
	elif [ "${PM}" == "apt-get" ]; then
		Pack="sudo"
		${PM} install ${Pack} -y
	fi

}
Service_Add(){
	if [ -f "/usr/bin/yum" ];then
		chkconfig --add redis
		chkconfig --level 2345 redis on
	elif [ -f "/usr/bin/apt" ]; then
		apt-get install sudo -y	
		update-rc.d redis defaults
	fi
}
Service_Del(){
	if [ -f "/usr/bin/yum" ];then
		chkconfig --level 2345 redis off
	elif [ -f "/usr/bin/apt" ]; then
		update-rc.d redis remove
	fi
}
Install_Redis()
{
	groupadd redis
	useradd -g redis -s /sbin/nologin redis
	if [ ! -f '/hcdemo/server/redis/src/redis-server' ];then
		rm -rf /hcdemo/server/redis
		cd /hcdemo/server
		wget $download_Url_BT/src/redis-$redis_version.tar.gz -T 5
		tar zxvf redis-$redis_version.tar.gz
		mv redis-$redis_version redis
		cd redis
		make -j ${cpuCore}

		wget -O /etc/init.d/redis ${download_Url_HC}/init.redis
		ln -sf /hcdemo/server/redis/src/redis-cli /usr/bin/redis-cli
		chmod +x /etc/init.d/redis
		chown -R redis.redis /hcdemo/server/redis
	
		sed -i 's/dir .\//dir \/hcdemo\/server\/redis\//g' /hcdemo/server/redis/redis.conf

		if [ -d "/hcdemo/server/panel/BTPanel" ]; then
			wget -O /etc/init.d/redis ${download_Url_HC}/init/init6.redis
			wget -O /hcdemo/server/redis/redis.conf ${download_Url_HC}/conf/redis.conf
		else
			wget -O /etc/init.d/redis ${download_Url_HC}/init/init5.redis
		fi

		chmod +x /etc/init.d/redis 
		/etc/init.d/redis start
		rm -f /hcdemo/server/redis-$redis_version.tar.gz
		cd $runPath
		echo $redis_version > /hcdemo/server/redis/version.pl
	fi
}

Uninstall_Redis()
{
	if [ ! -d /hcdemo/server/php/$version/bin ];then
		pkill -9 redis
		rm -f /var/run/redis_6379.pid
		Service_Del
		rm -f /usr/bin/redis-cli
		rm -f /etc/init.d/redis
		rm -rf /hcdemo/server/redis
		rm -rf /hcdemo/server/panel/plugin/redis

		return;
	fi
	if [ ! -f "/hcdemo/server/php/$version/bin/php-config" ];then
		echo "php-$vphp 未安装,请选择其它版本!"
		echo "php-$vphp not install, Plese select other version!"
		return
	fi
	
	isInstall=`cat /hcdemo/server/php/$version/etc/php.ini|grep 'redis.so'`
	if [ "${isInstall}" = "" ];then
		echo "php-$vphp 未安装Redis,请选择其它版本!"
		echo "php-$vphp not install Redis, Plese select other version!"
		return
	fi
	
	sed -i '/redis.so/d' /hcdemo/server/php/$version/etc/php.ini
	
	service php-fpm-$version reload
	echo '==============================================='
	echo 'successful!'
}
Update_redis(){ 
	REDIS_CONF="/hcdemo/server/redis/redis.conf"
	REDIS_PORT=$(cat ${REDIS_CONF} |grep port|grep -v '#'|awk '{print $2}')
	REDIS_PASS=$(cat ${REDIS_CONF} |grep requirepass|grep -v '#'|awk '{print $2}')
	REDIS_HOST=$(cat ${REDIS_CONF} |grep bind|grep -v '#'|awk '{print $2}')
	REDIS_DIR=$(cat ${REDIS_CONF} |grep dir|grep -v '#'|awk '{print $2}')

	cd /hcdemo/server
	
	wget $download_Url_BT/src/redis-$redis_version.tar.gz -T 5
	tar zxvf redis-$redis_version.tar.gz
	rm -f redis-$redis_version.tar.gz
	mv redis-$redis_version redis2
	cd redis2
	make -j ${cpuCore}
	if [ -f "${REDIS_DIR}dump.rdb" ]; then
		\cp -rf ${REDIS_DIR}dump.rdb ${REDIS_DIR}dumpBak 	 .rdb
		if [ -z "${REDIS_PASS}" ]; then
			/hcdemo/server/redis/src/redis-cli -p ${REDIS_PORT} <<EOF
SAVE
EOF
		else
			/hcdemo/server/redis/src/redis-cli -p ${REDIS_PORT} -a ${REDIS_PASS} <<EOF
SAVE
EOF
		fi
	fi

	/etc/init.d/redis stop
	sleep 1
	cd ..
	
	[ -f "/hcdemo/server/redis/dump.rdb" ] && \cp -rf /hcdemo/server/redis/dump.rdb /hcdemo/server/redis2/dump.rdb
	\cp -rf /hcdemo/server/redis/redis.conf /hcdemo/server/redis2/redis.conf

	if [ -d "/hcdemo/server/redisBak" ]; then
		tar czvf /hcdemo/backup/redisBak$(date +%Y%m%d).tar.gz /hcdemo/server/redisBak
		rm -rf /hcdemo/server/redisBak 
	fi

	mv /hcdemo/server/redis /hcdemo/server/redisBak
	mv redis2 redis
	chown -R redis.redis /hcdemo/server/redis
	rm -f /usr/bin/redis-cli
	ln -sf /hcdemo/server/redis/src/redis-cli /usr/bin/redis-cli
	/etc/init.d/redis start
	rm -f /hcdemo/server/redis/version_check.pl
	echo $redis_version > /hcdemo/server/redis/version.pl
}
actionType=$1
version=5.0.7
vphp=${version:0:1}.${version:1:1}
if [ "$actionType" == 'install' ];then
	System_Lib
	Install_Redis
	Service_Add
elif [ "$actionType" == 'uninstall' ];then
	Uninstall_Redis
elif [ "${actionType}" == "update" ]; then
	Update_redis
fi

